#include <stdlib.h>

#include "common.h"

gchar *encoding = NULL;

void
set_interface_encoding(gchar * enc)
{
	if (encoding != NULL)
		g_free(encoding);

	encoding = enc;
}


wchar_t *
wchar_from_utf8(const gchar * str, wchar_t * outbuf, gsize outbuf_size)
{
	gchar *locale_str;
	gsize bytes_read, bytes_written;

	if (str == NULL) {
		*outbuf = L'\0';
		return outbuf;
	}
	g_assert(g_utf8_validate(str, -1, NULL) == TRUE);

	locale_str =
		g_convert(str, -1, encoding, "UTF-8", &bytes_read,
			  &bytes_written, NULL);
	g_assert(locale_str != NULL);

	mbstowcs(outbuf, locale_str, outbuf_size);
	g_free(locale_str);

	outbuf[outbuf_size] = L'\0';

	return outbuf;
}
